﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DAL;
using HomeStayWebCP.Models.BusinessObject;
using System.Globalization;
using System.Text.RegularExpressions;

namespace HomeStayWebCP.Models
{
    public class CommentModel
    {
        #region Hoangdx Code Comment

        private int pageNumber;
        private int countNo;

        private int CheckPage(String page)
        {
            int curpage = 1;
            if (page != null)
            {
                if (Regex.IsMatch(page, "[0-9]+"))
                {
                    curpage = int.Parse(page.Trim());
                    if (curpage > countNo)
                    {
                        curpage = countNo;
                    }
                    if (curpage < 1)
                    {
                        curpage = 1;
                    }
                }
            }
            return curpage;
        }

        public List<CommentObject> GetCommentOfPost(int postID)
        {
            List<CommentObject> listCommentResult = new List<CommentObject>();
            CommentsDataModel model = new CommentsDataModel();
            List<Store_Comment_GetCommentsOfPost_Result> commentlist = model.GetCommentsOfPost(postID);
            foreach (var commentDS in commentlist)
            {
                CommentObject commentDetail = new CommentObject();
                commentDetail.CommentID = commentDS.CommentID;
                commentDetail.Content = commentDS.Content.Replace(System.Environment.NewLine, "<br />");
                commentDetail.UserName = commentDS.Username;
                commentDetail.DateComment = commentDS.TimeCreated;
                commentDetail.rows = commentDS.Row;

                listCommentResult.Add(commentDetail);
            }
            return listCommentResult;
        }

        public List<CommentObject> listCommentResult { get; set; }

        public int SetCountPaging(int rownumber, int postID)
        {
            pageNumber = rownumber;
            listCommentResult = new List<CommentObject>();
            listCommentResult = GetCommentOfPost(postID);

            int countrows = listCommentResult.Count;
            countNo = countrows % pageNumber == 0 ? countrows / pageNumber : countrows / pageNumber + 1;
            return countNo;
        }

        public List<CommentObject> GetCommentOfPaing(ref String pageindex, int postID)
        {
            List<CommentObject> listCommentPaging = new List<CommentObject>();
            int page = CheckPage(pageindex);//kiểm tra trang đúng cú pháp chưa
            pageindex = page.ToString(CultureInfo.InvariantCulture);//set lại giá trị sau khi kiểm tra
            long? start = page * pageNumber - pageNumber;
            long? end = page * pageNumber;

            listCommentPaging = listCommentResult.Where(o => o.rows > start && o.rows <= end).ToList();

            return listCommentPaging;
        }

        public void InsertComments(CommentObject commentDTO)
        {
            DAL.CommentsDataModel model = new CommentsDataModel();
            DAL.Comment commentDB = new Comment()
            {
                Content = commentDTO.Content,
                Username = commentDTO.UserName,
                PostID = commentDTO.PostID
            };
            model.InsertComment(commentDB);

            //return GetCommentOfPost(commentDTO.PostID);
        }

        #endregion

        public void DeleteComment(int commentId)
        {
            CommentsDataModel dataModel = new CommentsDataModel();
            dataModel.DeleteComment(commentId);

        }
    }
}